package com.menghao.sso.client.config;

import com.menghao.sso.client.filter.AuthenticationFilter;
import com.menghao.sso.client.filter.CheckTicketFilter;
import com.menghao.sso.client.filter.WrapInfoFilter;
import com.menghao.sso.client.validation.PersonTicketValidator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

/**
 * <p>客户端自动配置类.<br>
 *
 * @author menghao.
 * @version 2017/11/16.
 */
@Configuration
@EnableConfigurationProperties(SsoClientProperties.class)
@ConditionalOnWebApplication
@ConditionalOnProperty(prefix = "menghao.sso", value = "enabled", matchIfMissing = false)
public class SsoClientAutoConfiguration {

    private SsoClientProperties ssoClientProperties;

    private List<String> urls;

    public SsoClientAutoConfiguration(SsoClientProperties ssoClientProperties) {
        this.ssoClientProperties = ssoClientProperties;
        String strictUrls = ssoClientProperties.getRestrictUrls();
        Assert.hasText(ssoClientProperties.getClientHost(), "服务主机地址必须指定");
        Assert.hasText(strictUrls, "拦截地址必须指定");
        // 初始化时，会将配置需要拦截的url分割成列表
        urls = Arrays.asList(strictUrls.split(","));
    }

    @Bean
    @ConditionalOnMissingBean(AuthenticationFilter.class)
    public FilterRegistrationBean registerAuthenticationFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        AuthenticationFilter authenticationFilter = new AuthenticationFilter();
        authenticationFilter.setServerHost(ssoClientProperties.getServerHost());
        authenticationFilter.setClientHost(ssoClientProperties.getClientHost());
        filterRegistrationBean.setFilter(authenticationFilter);
        // 配置过滤器需要拦截的url列表
        filterRegistrationBean.setUrlPatterns(urls);
        filterRegistrationBean.setOrder(1);
        return filterRegistrationBean;
    }

    @Bean
    @ConditionalOnMissingBean(CheckTicketFilter.class)
    public FilterRegistrationBean registerCheckTicketFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        CheckTicketFilter checkTicketFilter = new CheckTicketFilter();
        checkTicketFilter.setServerHost(ssoClientProperties.getServerHost());
        checkTicketFilter.setClientHost(ssoClientProperties.getClientHost());
        checkTicketFilter.setTicketValidator(new PersonTicketValidator(new RestTemplate()));
        filterRegistrationBean.setFilter(checkTicketFilter);
        filterRegistrationBean.setUrlPatterns(urls);
        filterRegistrationBean.setOrder(2);
        return filterRegistrationBean;
    }

    @Bean
    @ConditionalOnMissingBean(WrapInfoFilter.class)
    public FilterRegistrationBean registerWrapInfoFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WrapInfoFilter());
        filterRegistrationBean.setUrlPatterns(urls);
        filterRegistrationBean.setOrder(3);
        return filterRegistrationBean;
    }

}
